﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace YTCMS.Core.Security
{
    public sealed class FigCryptographer
    {
        //public static string Encrypt(string text)
        //{
        //    string result;
        //    using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
        //    {
        //        rijndaelManaged.Key = Convert.FromBase64String(ConfigurationManager.AppSettings["Key"]);
        //        rijndaelManaged.IV = Convert.FromBase64String(ConfigurationManager.AppSettings["IV"]);
        //        ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor();
        //        byte[] bytes = Encoding.UTF8.GetBytes(text);
        //        byte[] inArray = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length);
        //        cryptoTransform.Dispose();
        //        result = Convert.ToBase64String(inArray);
        //    }
        //    return result;
        //}
        public static string Decrypt(string text)
        {
            string @string;
            using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
            {
                rijndaelManaged.Key = Convert.FromBase64String(ConfigurationManager.AppSettings["Key"]);
                rijndaelManaged.IV = Convert.FromBase64String(ConfigurationManager.AppSettings["IV"]);
                ICryptoTransform cryptoTransform = rijndaelManaged.CreateDecryptor();
                byte[] array = Convert.FromBase64String(text);
                byte[] bytes = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
                cryptoTransform.Dispose();
                @string = Encoding.UTF8.GetString(bytes);
            }
            return @string;
        }
        //private static byte[] a(byte[] A_0)
        //{
        //    MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
        //    return mD5CryptoServiceProvider.ComputeHash(A_0);
        //}
        //public static string CreateHash(string plaintext)
        //{
        //    byte[] array = a(Encoding.ASCII.GetBytes(plaintext));
        //    StringBuilder stringBuilder = new StringBuilder();
        //    for (int i = 0; i < array.Length; i++)
        //    {
        //        stringBuilder.Append(array[i].ToString("x2"));
        //    }
        //    return stringBuilder.ToString();
        //}

        public static string EncodePassword(string pass, int passwordFormat, string salt)
        {
            return EncodePassword(pass, salt);
            //if (passwordFormat == 0)
            //{
            //    return pass;
            //}
            //byte[] bytes = Encoding.Unicode.GetBytes(pass);
            //byte[] src = Convert.FromBase64String(salt);
            //byte[] dst = new byte[src.Length + bytes.Length];
            //byte[] inArray = null;
            //Buffer.BlockCopy(src, 0, dst, 0, src.Length);
            //Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
            //inArray = HashAlgorithm.Create("SHA256").ComputeHash(dst);
            //return Convert.ToBase64String(inArray);
        }

        private static string EncodePassword(string pass,string salt)
        {
            pass = salt + pass;
            byte[] bytes = Encoding.Default.GetBytes(pass);
            byte[] inArray = null;
            inArray = HashAlgorithm.Create("SHA256").ComputeHash(bytes);
            return ToHexString(inArray);
        }

        public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();

                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();
            }
            return hexString;
        }

    }
}
